/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file    tim.c
 * @brief   This file provides code for the configuration
 *          of the TIM instances.
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2024 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "tim.h"

/* USER CODE BEGIN 0 */
#include "usart.h"

TIM_ClockConfigTypeDef tim1_clock;
TIM_OnePulse_InitTypeDef tim1_op; // 单脉冲控制结构体
uint16_t tim2_dmabuff[4] = {6, 7, 8, 9};
uint16_t tim1_dmabuff[4] = {6, 7, 8, 9};
uint16_t counter = 0;

uint32_t sw8_dowm = 0;
uint32_t sw8_up = 0;
uint32_t sw11_dowm = 0;
uint32_t sw11_up = 0;
uint32_t sw8_count = 0;
uint32_t sw9_count = 0;
uint32_t sw10_count = 0;
uint32_t sw11_count = 0;
// TIM2 DMA CCR
uint16_t tim2_ic1_dmabuff[10];
uint16_t tim2_ic2_dmabuff[10];

uint32_t old_num = 0xFFFFFFFF;

uint16_t tim1_oc1_pwmdmabuff[4] = {200, 300, 400, 500}; // 定时器1的通道1的DMA的数组
uint16_t tim1_oc2_pwmdmabuff[4] = {300, 500, 700, 900}; // 定时器1的通道2的DMA的数组
uint16_t tim1_oc3_pwmdmabuff[4] = {600, 700, 800, 900}; // 定时器1的通道3的DMA的数组

uint32_t tim1_counter = 0; // 用于记录定时器1的更新次数
uint32_t tim1_number = 0;  // 用于记录定时器1的初值

uint32_t tim2_counter = 0; // 用于记录定时器2的更新次数
uint32_t tim2_number = 0;  // 用于记录定时器2的初值

uint32_t tim3_counter = 0; // 用于记录定时器2的更新次数
uint32_t tim3_number = 0;  // 用于记录定时器2的初值

uint32_t tim4_counter = 0; // 用于记录定时器2的更新次数
uint32_t tim4_number = 0;  // 用于记录定时器2的初值

/* USER CODE END 0 */

TIM_HandleTypeDef htim2;
TIM_HandleTypeDef htim3;

/* TIM2 init function */
void MX_TIM2_Init(void)
{

  /* USER CODE BEGIN TIM2_Init 0 */

  /* USER CODE END TIM2_Init 0 */

  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};

  /* USER CODE BEGIN TIM2_Init 1 */

  /* USER CODE END TIM2_Init 1 */
  htim2.Instance = TIM2;
  htim2.Init.Prescaler = 36000-1;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 2000-1;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM2_Init 2 */
  __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_UPDATE);                                 // 清除中断
 	HAL_TIM_Base_Start_IT(&htim2);
  /* USER CODE END TIM2_Init 2 */

}
/* TIM3 init function */
void MX_TIM3_Init(void)
{

  /* USER CODE BEGIN TIM3_Init 0 */

  /* USER CODE END TIM3_Init 0 */

  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_SlaveConfigTypeDef sSlaveConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};

  /* USER CODE BEGIN TIM3_Init 1 */

  /* USER CODE END TIM3_Init 1 */
  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 36000-1;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 4000-1;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sSlaveConfig.SlaveMode = TIM_SLAVEMODE_RESET;
  sSlaveConfig.InputTrigger = TIM_TS_ITR1;
  if (HAL_TIM_SlaveConfigSynchro(&htim3, &sSlaveConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM3_Init 2 */
	__HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE);                                 // 清除中断
	__HAL_TIM_URS_ENABLE(&htim3); 
 	HAL_TIM_Base_Start_IT(&htim3);
  /* USER CODE END TIM3_Init 2 */

}

void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{

  if(tim_baseHandle->Instance==TIM2)
  {
  /* USER CODE BEGIN TIM2_MspInit 0 */

  /* USER CODE END TIM2_MspInit 0 */
    /* TIM2 clock enable */
    __HAL_RCC_TIM2_CLK_ENABLE();

    /* TIM2 interrupt Init */
    HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(TIM2_IRQn);
  /* USER CODE BEGIN TIM2_MspInit 1 */

  /* USER CODE END TIM2_MspInit 1 */
  }
  else if(tim_baseHandle->Instance==TIM3)
  {
  /* USER CODE BEGIN TIM3_MspInit 0 */

  /* USER CODE END TIM3_MspInit 0 */
    /* TIM3 clock enable */
    __HAL_RCC_TIM3_CLK_ENABLE();

    /* TIM3 interrupt Init */
    HAL_NVIC_SetPriority(TIM3_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(TIM3_IRQn);
  /* USER CODE BEGIN TIM3_MspInit 1 */

  /* USER CODE END TIM3_MspInit 1 */
  }
}

void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
{

  if(tim_baseHandle->Instance==TIM2)
  {
  /* USER CODE BEGIN TIM2_MspDeInit 0 */

  /* USER CODE END TIM2_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_TIM2_CLK_DISABLE();

    /* TIM2 interrupt Deinit */
    HAL_NVIC_DisableIRQ(TIM2_IRQn);
  /* USER CODE BEGIN TIM2_MspDeInit 1 */

  /* USER CODE END TIM2_MspDeInit 1 */
  }
  else if(tim_baseHandle->Instance==TIM3)
  {
  /* USER CODE BEGIN TIM3_MspDeInit 0 */

  /* USER CODE END TIM3_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_TIM3_CLK_DISABLE();

    /* TIM3 interrupt Deinit */
    HAL_NVIC_DisableIRQ(TIM3_IRQn);
  /* USER CODE BEGIN TIM3_MspDeInit 1 */

  /* USER CODE END TIM3_MspDeInit 1 */
  }
}

/* USER CODE BEGIN 1 */

/**
 * @brief  Input Capture callback in non-blocking mode  DMA完成回调也是调用这个
 * @param  htim TIM IC handle
 * @retval None
 */
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
  uint8_t i;
  uint32_t num1, num2;

  if (htim->Instance == TIM2)
  {
    if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
    {
      num1 = 0;
      num2 = 0;
      for (i = 0; i < 10; i++) // 10次DMA 做一次运算
      {
        num1 += tim2_ic1_dmabuff[i];   //CCR1  
        num2 += tim2_ic2_dmabuff[i];   //CCR2
      }

      u1_printf("period: %f hz\r\n", 1 / (( num2/10.0 / 72.0) / 1000 / 1000));           // 1计数 1/72  us
      u1_printf("duty cycle: %f \r\n", 100.0 * (num1/10.0) / (num2/10.0) );                         // 触发中断时 高电平时间已经在CCR2 中 总时间在CCR1
    }
  }
}

/**
 * @brief 定时器更新中断
 *
 * @param htim
 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  if (htim->Instance == TIM1)
  {
    u1_printf("time1 update it %d\r\n", ++tim1_counter);
  }
  else if (htim->Instance == TIM2)
  {
    u1_printf("time2 update it %d\r\n", ++tim2_counter);
  }
  else if (htim->Instance == TIM3)
  {
    u1_printf("time3 update it %d\r\n", ++tim3_counter);
  }
  else if (htim->Instance == TIM4)
  {
    u1_printf("time4 update it %d\r\n", ++tim4_counter);
  }
}

/**
 * @brief 定时器触发中断
 *
 * @param htim
 */
void HAL_TIM_TriggerCallback(TIM_HandleTypeDef *htim)
{
  if (htim->Instance == TIM1)
  {
    u1_printf("time1 trigger int\r\n");
  }
  else if (htim->Instance == TIM2)
  {
    u1_printf("time2 trigger int\r\n");
  }
  else if (htim->Instance == TIM3)
  {
    u1_printf("time3 trigger int\r\n");
  }
  else if (htim->Instance == TIM4)
  {
    u1_printf("time4 trigger int\r\n");
  }
}

/* USER CODE END 1 */
